package kz.pompei.OneGwt.client.personrole.impl;

import kz.pompei.OneGwt.client.personrole.PersonListView;
import kz.pompei.OneGwt.cmodel.PersonRecord;
import kz.pompei.OneGwt.ifaces.util.Getter;
import kz.pompei.OneGwt.ifaces.util.Transpazon;
import kz.pompei.OneGwt.ifaces.widget.ButtonWidget;
import kz.pompei.OneGwt.ifaces.widget.ListWidget;
import kz.pompei.OneGwt.ifaces.widget.NullAdapters;
import kz.pompei.OneGwt.ifaces.widget.gwtadapters.Button_impl_ButtonWidget;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Вьюшка "Список пользователей и управление им"
 * 
 * @author pompei
 */
public class PersonListViewImpl implements PersonListView {
  private Widget containerUI;
  
  private ButtonWidget buttonAdd = NullAdapters.noButton();
  private ButtonWidget buttonDelete = NullAdapters.noButton();
  private ButtonWidget buttonEdit = NullAdapters.noButton();
  private ListWidget<PersonRecord> listPerson = NullAdapters.noListWidget();
  
  private final Transpazon<UIObject> personListSource = new Transpazon<UIObject>();
  
  public Widget getContainerUI() {
    if (containerUI == null) containerUI = createContainerUI();
    return containerUI;
  }
  
  private Widget createContainerUI() {
    HorizontalPanel wall = new HorizontalPanel();
    wall.setSpacing( 5 );
    {
      VerticalPanel forBtns = new VerticalPanel();
      wall.add( forBtns );
      wall.setCellWidth( forBtns, "1px" );
      {
        Button b = new Button( "+" );
        b.setWidth( "2em" );
        forBtns.add( b );
        
        buttonAdd = new Button_impl_ButtonWidget( b );
      }
      {
        Button b = new Button( "..." );
        b.setWidth( "2em" );
        forBtns.add( b );
        
        buttonEdit = new Button_impl_ButtonWidget( b );
      }
      {
        Button b = new Button( "-" );
        b.setWidth( "2em" );
        forBtns.add( b );
        
        buttonDelete = new Button_impl_ButtonWidget( b );
      }
    }
    {
      final ListBox listBox = new ListBox();
      listBox.setWidth( "100%" );
      listBox.setVisibleItemCount( 20 );
      wall.add( listBox );
      wall.setCellWidth( listBox, "100%" );
      
      listPerson = new PersonListWidget( listBox );
      
      personListSource.setGetter( new Getter<UIObject>() {
        @Override
        public UIObject get() {
          return listBox;
        }
      } );
    }
    
    return wall;
  }
  
  @Override
  public ButtonWidget buttonAdd() {
    return buttonAdd;
  }
  
  @Override
  public ButtonWidget buttonDelete() {
    return buttonDelete;
  }
  
  @Override
  public ButtonWidget buttonEdit() {
    return buttonEdit;
  }
  
  @Override
  public ListWidget<PersonRecord> listPerson() {
    return listPerson;
  }
  
  public Transpazon<UIObject> getPersonListSource() {
    return personListSource;
  }
  
}
